package com.pwc.GAE.blobstore;

import java.io.IOException;
import java.util.List;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreInputStream;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class BlobstoreUtil {
	
	private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();	
	
	
	/**
	 * create a upload url for file upload
	 * @param path
	 * @return gae path
	 */
	public static String createUploadUrl(String path)
	{
		
		return blobstoreService.createUploadUrl(path);
	}
	

	
	public static BlobstoreInputStream getBlobstoreInputStream(BlobKey key) throws IOException
	{
		BlobstoreInputStream in =new BlobstoreInputStream(key);
		return in;
	}
	
	public static BlobstoreInputStream getBlobstoreInputStream(String key) throws IOException
	{
		BlobKey blobKey=new BlobKey(key);
		return getBlobstoreInputStream(blobKey);
	}
	
	public static BlobInfo getBlobInfo(BlobKey key) 
	{
		BlobInfo info =new BlobInfoFactory().loadBlobInfo(key);
		return info;
	}
	
	public static BlobInfo getBlobInfo(String key) 
	{
		BlobKey blobKey=new BlobKey(key);
		return getBlobInfo(blobKey);
	}
	
	
	
	public static void deleteBlob(BlobKey key)
	{
		blobstoreService.delete(key);		
	}
	
	public static void deleteBlob(String key)
	{
		BlobKey blobKey=new BlobKey(key);
		blobstoreService.delete(blobKey);		
	}
	
	
	public static void deleteBlob(List<String> key)
	{
		for (String string : key) {
			deleteBlob(string);
		}
	}

}
